import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../core/auth_store.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:flutter/foundation.dart' show defaultTargetPlatform, TargetPlatform, kIsWeb;
import 'package:url_launcher/url_launcher.dart';
import 'history_page.dart';
import 'set_page.dart';
import 'about_page.dart';
import '../core/theme_config.dart';
import 'version_check_page.dart';
import 'home_page.dart';
import '../utils/util.dart';
import 'account_page.dart';

class ProfilePage extends StatefulWidget {
  const ProfilePage({super.key});

  @override
  State<ProfilePage> createState() => _ProfilePageState();
}

class _ProfilePageState extends State<ProfilePage> {
  int _selectedTab = 0;

  @override
  Widget build(BuildContext context) {
    final themeConfig = Provider.of<ThemeConfig>(context);
    final auth = Provider.of<AuthStore>(context);
    return Scaffold(
      appBar: AppBar(
        title: Text(auth.loggedIn ? (auth.username ?? '个人中心') : '个人中心'),
        toolbarHeight: 32,
        centerTitle:  true,
        backgroundColor: themeConfig.currentGradient.colors.first, 
        //字体颜色
        titleTextStyle: TextStyle(fontSize: 18,color: themeConfig.currentTextColor),
        elevation: 0,
      ),
      body: Container(
        decoration: BoxDecoration(
          gradient: themeConfig.currentGradient,
        ),
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            // 左侧纵向菜单
            Container(
              //设置当窄屏时宽度为80，宽屏时为120
              width: MediaQuery.of(context).size.width < 600 ? 80 : 120,
              padding: const EdgeInsets.symmetric(vertical: 12, horizontal: 12),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.stretch,
                children: [
                  // _buildVerticalTab('账号', 0, themeConfig),
                  const SizedBox(height: 4),
                  _buildVerticalTab('记录', 0, themeConfig),
                  const SizedBox(height: 4),
                  _buildVerticalTab('设置', 1, themeConfig),
                  const SizedBox(height: 4),
                  _buildVerticalTab('版本检测', 2, themeConfig),
                  const SizedBox(height: 4),
                  _buildVerticalTab('关于', 3, themeConfig),
                ],
              ),
            ),
            const VerticalDivider(width: 1),
            // 右侧内容区域
            Expanded(
              child: IndexedStack(
                index: _selectedTab,
                children: const [
                  // AccountPage(),
                  HistoryPage(),
                  SetPage(),
                  VersionCheckPage(),
                  AboutPage(),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }

  // 纵向菜单项
  Widget _buildVerticalTab(String label, int index, ThemeConfig themeConfig) {
    final selected = _selectedTab == index;
    return InkWell(
      onTap: () => setState(() => _selectedTab = index),
      borderRadius: BorderRadius.circular(8),
      child: Container(
        padding: const EdgeInsets.symmetric(vertical: 10, horizontal: 12),
        decoration: BoxDecoration(
          color: selected ? themeConfig.currentTextColor.withOpacity(0.08) : Colors.transparent,
          borderRadius: BorderRadius.circular(8),
        ),
        child: Text(
          label,
          style: TextStyle(
            fontWeight: selected ? FontWeight.w600 : FontWeight.normal,
            color: themeConfig.currentTextColor,
          ),
        ),
      ),
    );
  }

  Widget _buildTab(String label, int index, ThemeConfig themeConfig) {
    final selected = _selectedTab == index;
    return Expanded(
      child: InkWell(
        onTap: () => setState(() => _selectedTab = index),
        child: Container(
          padding: const EdgeInsets.symmetric(vertical: 16),
          decoration: BoxDecoration(
            border: Border(
              bottom: BorderSide(
                color: themeConfig.currentTextColor,
                width: 2,
              ),
            ),
          ),
          child: Text(
            label,
            textAlign: TextAlign.center,
            style: TextStyle(
              fontWeight: selected ? FontWeight.w600 : FontWeight.normal,
               color:themeConfig.currentTextColor,
            ),
          ),
        ),
      ),
    );
  }
}
